package com.zhuoyue.biz.core.api.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuoyue.biz.core.api.domain.custom.GeofencingMessageDO;
import com.zhuoyue.biz.core.api.domain.custom.HomeAndSceneDO;
import com.zhuoyue.biz.core.api.dto.rep.GeofencingRepDTO;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuoyue.biz.core.api.domain.Geofencing;
import com.zhuoyue.biz.core.api.dao.GeofencingMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author wjming
 * @ClassName: GeofencingService
 * @Description: 地理围栏服务
 * @Date 2021-07-06 13:22
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GeofencingService extends ServiceImpl<GeofencingMapper, Geofencing> {
    private final GeofencingMapper geofencingMapper;

    /**
     * 地理围栏配置列表
     * 获取地理围栏列表
     * 转化为DTO
     *
     * @param gatewayId
     * @return
     */
    public List<GeofencingRepDTO> getMessageList(String gatewayId) {
        List<Geofencing> geofencingList = list(Wrappers.<Geofencing>lambdaQuery().eq(Geofencing::getGatewayId, gatewayId));
        if (CollectionUtils.isEmpty(geofencingList)) {
            return Collections.emptyList();
        }
        return createGeofencingRepDtoList(gatewayId);
    }

    /**
     * 添加地理围栏信息
     *
     * @param geofencing
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public List<GeofencingRepDTO> addMessage(List<Geofencing> geofencing) {
        boolean save = saveBatch(geofencing);
        if (!save) {
            throw new ZyServerException(ErrorType.SAVE_ERROR);
        }
        return createGeofencingRepDtoList(geofencing.get(0).getGatewayId());
    }

    /**
     * 修改地理围栏信息
     *
     * @param entity
     */
    @Transactional(rollbackFor = Exception.class)
    public List<GeofencingRepDTO> updateMessage(List<Geofencing> entity) {
        boolean b = updateBatchById(entity);
        if (!b) {
            throw new ZyServerException(ErrorType.UPDATE_ERROR);
        }
        Geofencing byId = getById(entity.get(0).getId());
        return createGeofencingRepDtoList(byId.getGatewayId());
    }

    /**
     * 删除数据信息
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    public void removeMessage(List<String> id) {
        boolean b = removeByIds(id);
        if (!b) {
            throw new ZyServerException(ErrorType.REMOVE_ERROR);
        }
    }

    /**
     * 创建GeofencingRep的List回应对象
     *
     * @param gatewayId
     * @return
     */
    private List<GeofencingRepDTO> createGeofencingRepDtoList(String gatewayId) {
        List<GeofencingMessageDO> message = geofencingMapper.selectHomeAndSceneMessage(gatewayId);
        if (CollectionUtils.isEmpty(message)) {
            return Collections.emptyList();
        }
        List<GeofencingRepDTO> geofencingReps = new ArrayList<>(2);
        message.forEach(list -> {
            GeofencingRepDTO reps = new GeofencingRepDTO().geofencingToRepDTO(list);
            geofencingReps.add(reps);
        });
        return geofencingReps;
    }
}



